/**
 * MusicService.java  [V 1.0.0]
 * classpath : com.example.learnservice.MusicService
 * zhangyw Create at 2015年4月9日 下午3:55:16
 */
package com.example.learnservice;

import android.annotation.TargetApi;
import android.app.Service;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnPreparedListener;
import android.net.Uri;
import android.os.Build;
import android.os.IBinder;
import android.util.Log;

/**
 * com.example.learnservice.MusicService
 * @author zhangyw
 * create at 2015年4月9日 下午3:55:16
 */
public class MusicService extends Service{
  private static final String TAG = "MusicService";
  public static final String STOP_IT = "com.example.learnservice.AduioStopPlaying";
  public static final String PLAY_ADUIO = "com.example.learnservice.AduioStartPlaying";
  private MediaPlayer mediaPlayer;
  /**
   * 
   */
  public MusicService() {
  }
  
  
  /* (non-Javadoc)
   * @see android.app.Service#onStartCommand(android.content.Intent, int, int)
   */
  @Override
  public int onStartCommand(Intent intent, int flags, int startId) {
    Log.w("note", "service running ");
    if(intent!=null){
      //先处理之前的状态
      if(mediaPlayer!=null){
        mediaPlayer.release();
      }
      mediaPlayer = MediaPlayer.create(getBaseContext(),intent.getData());
//      mediaPlayer.prepareAsync();
      if(mediaPlayer!=null){
        mediaPlayer.setOnPreparedListener(new OnPreparedListener() {
          
          @Override
          public void onPrepared(MediaPlayer mp) {
            mp.start();
          }
        });
      }

    }
    return Service.START_STICKY;
  }
  
  /* (non-Javadoc)
   * 我认为应该要在设置了action filter之后才能执行该方法
   * @see android.content.ContextWrapper#stopService(android.content.Intent)
   */
  @Override
  public boolean stopService(Intent name) {
    if(mediaPlayer!=null){
      mediaPlayer.stop();
      mediaPlayer.release();
    }
    Log.v("note", "stop service begin");
    return super.stopService(name);
  }
  
  

  /* (non-Javadoc)
   * @see android.app.Service#onCreate()
   */
  @Override
  public void onCreate() {
    super.onCreate();
  }

  /* (non-Javadoc)
   * @see android.app.Service#onDestroy()
   */
  @Override
  public void onDestroy() {
    Log.v("note", "onDestroy begin");
    mediaPlayer.release();
    super.onDestroy();
  }
  
  /* (non-Javadoc)
   * @see android.app.Service#onUnbind(android.content.Intent)
   */
  @Override
  public boolean onUnbind(Intent intent) {
    Log.v("note", "onUnbind begin");
    return super.onUnbind(intent);
  }
  
  /* (non-Javadoc)
   * @see android.app.Service#onTaskRemoved(android.content.Intent)
   */
  @TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
  @Override
  public void onTaskRemoved(Intent rootIntent) {
    Log.v("note", "onTaskRemoved begin");
    super.onTaskRemoved(rootIntent);
  }

  /* (non-Javadoc)
   * @see android.app.Service#onBind(android.content.Intent)
   */
  @Override
  public IBinder onBind(Intent intent) {
    Log.v("note", "onBind begin");
    return null;
  }
}
